package com.alibaba.utils.qrcode;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author com.alibaba.lizhen
 * @Date 2021/01/12 7:13 下午
 * @Version 1.0
 * @Description 测试
 */
public class QrcodeUtilsTest {
    private static Logger logger = LoggerFactory.getLogger(QrcodeUtils.class);

    private String content = "http://www.circle.net.cn/ble/admin/manager.html?alarmId=";
    private List<Path> generatedQrcodePaths = Lists.newArrayList();

    private final String IMAGE_DIR = "/Users/lizhen/developTools/ideaProject/tools/qrcode-utils/images/";

    private String name = "aaa";

    private final String PREFIX_800 = "qrcode_800_"; //35k,  6400内存达1M
    private final String PREFIX_400 = "qrcode_400_"; //15k
    private final String SUFFIX_JPG = ".jpg";
    private final String SUFFIX_PNG = ".png";

    @Before
    public void setup() {
        BasicConfigurator.configure();
    }


    @Test
    public void testCreateQrcodeInPath() throws Exception {
        final Integer id = 11110001;
        int count = 25;
//        final int[] pixs = {400, 800, 1600, 3200};
//        final int[] pixs = {6400, 6400, 6400, 6400, 6400};
        long begin = System.currentTimeMillis();
        ThreadPoolExecutor executor = ThreadUtils.getExecutor();
        for (int i = 0; i < count; i++) {
            long startTime = System.currentTimeMillis();
            final int index = i;
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        final int tempId = id + index;
                        String ctx = content + "" + tempId;
                        //生成二维码，第二个参数是像素
                        byte[] bytes = QrcodeUtils.createQrcode(ctx, 800, null);
//                        String path = IMAGE_DIR + "Circle" + pixs[index] + "_" + name + SUFFIX_JPG;
                        String path = IMAGE_DIR + "Alarm_Circle" + "_" + tempId + SUFFIX_PNG;
                        new FileOutputStream(path).write(bytes);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        executor.shutdown();
        //判断是否所有任务被执行完
        while (!executor.isTerminated()) {
            logger.info("剩余任务数 :" + executor.getQueue().size());
            Thread.sleep(1000);
        }
        logger.info("总耗时 :" + (System.currentTimeMillis() - begin));
    }

    @Test
    public void testCreateQrcode() throws Exception {
        byte[] bytes = QrcodeUtils.createQrcode(content, 800, null);
        Path path = Files.createTempFile("qrcode_800_", ".jpg");
        generatedQrcodePaths.add(path);
        logger.info("{}", path.toAbsolutePath());
        Files.write(path, bytes);

        bytes = QrcodeUtils.createQrcode(content, null);
        path = Files.createTempFile("qrcode_400_", ".jpg");
        generatedQrcodePaths.add(path);
        logger.info("{}", path.toAbsolutePath());
        Files.write(path, bytes);
    }

    @Test
    public void testCreateQrcodeWithLogo() throws Exception {
        try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("logo.png")) {
            File logoFile = Files.createTempFile("logo_", ".jpg").toFile();
            FileUtils.copyInputStreamToFile(inputStream, logoFile);
            logger.info("{}", logoFile);
            byte[] bytes = QrcodeUtils.createQrcode(content, 800, logoFile);
            Path path = Files.createTempFile("qrcode_with_logo_", ".jpg");
            generatedQrcodePaths.add(path);
            logger.info("{}", path.toAbsolutePath());
            Files.write(path, bytes);
        }
    }

//    @Test(dependsOnMethods = {"testCreateQrcode", "testCreateQrcodeWithLogo"})
//    public void testDecodeQrcode() throws Exception {
//        for (Path path : generatedQrcodePaths) {
//            Assert.assertEquals(QrcodeUtils.decodeQrcode(path.toFile()), content);
//        }
//    }


}
